--
-- Author:      feilong
-- DateTime:    2018-04-23 17:19:33
-- Description: 日志服务

--
local skynet = require "skynet"
require "skynet.manager"

local MessageDispatch = require "MessageDispatch"
local MessageHandler = require "logger.MessageHandler"


--服务中唯一全局变量
global = {}


local function init()

	local message_dispatch = MessageDispatch.new()	
	local message_handler = MessageHandler.new(message_dispatch)
	local server_name = skynet.getenv("server_name")
	-- skynet.dispatch("lua", message_dispatch:dispatch())

	--收到skynet.error消息 		
	skynet.register_protocol {
		name = "text",
		id = skynet.PTYPE_TEXT,
		unpack = skynet.tostring,
		dispatch = function(_, address, msg)
			--收到消息 					
			local str = string.format("[:%08x] %s", address, msg)	
			print(server_name.."]"..str)		
			message_handler:saveData(str)
		end		
	}	
	skynet.register_protocol {
		name = "SYSTEM",
		id = skynet.PTYPE_SYSTEM,
		unpack = function(...) return ... end,
		dispatch = function()
			-- reopen signal
			print("SIGHUP")
		end
	}
end


---------------------------------------------------------
-- skynet
---------------------------------------------------------
init() 

skynet.start(function()
end)
